package com.jh.paymentchannel.payment;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.io.Serializable;
import java.math.BigDecimal;

import static io.jsonwebtoken.lang.Strings.hasText;

/**
 * @author Yichuan
 * @version 1.0
 */
public class PaymentRequest implements Serializable {

    private static final long serialVersionUID = 2560263921726767779L;

    // 支付金额
    private BigDecimal amount;
    // 台码
    private String platformCode;
    // 支付渠道(alipay wechat)
    private String channel;
    // 微信请求必传(用来获取open_id)
    private String code;

    private transient String ip;

    public PaymentRequest() {
        super();
    }

    public BigDecimal getAmount() {
        return amount;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }

    public String getPlatformCode() {
        return platformCode;
    }

    public void setPlatformCode(String platformCode) {
        this.platformCode = platformCode;
    }

    public String getChannel() {
        return channel;
    }

    public void setChannel(String channel) {
        this.channel = channel;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @JsonIgnore
    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String valid() {
        if ((amount == null || amount.compareTo(BigDecimal.ZERO) <= 0)) {
            return "支付金额不能为空且必须大于0";
        }
        if (!hasText(platformCode)) {
            return "支付金额不能为空且必须大于0";
        }
        if (!hasText(channel) || (!isAlipay() && !isWechat())) {
            return "支付渠道信息错误";
        }
        if (isWechat() && !hasText(code)) {
            return "请返回微信code";
        }
        return null;
    }

    public OrderType getOrderType() {
        return OrderType.of(channel);
    }

    public boolean isAlipay() {
        return getChannel().equalsIgnoreCase("alipay");
    }

    public boolean isWechat() {
        return getChannel().equalsIgnoreCase("wechat");
    }

    public String getParamUrl() {
        return "?data=" + getAmount() + "-" + getPlatformCode();
    }
}
